#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>


void * start_routine(void * arg)
{
    printf("sub thread is running.\n");
    printf("sub thread id is %ld\n", pthread_self());
    //在子线程中申请堆空间的资源
    int * pVal = (int*)malloc(sizeof(int));
    *pVal = 10;//修改指向的堆空间的数据
    printf("sub thread pVal: %p\n", pVal);

    /* sleep(5);//休眠5秒钟, 单位为秒 */

    /* return NULL;//返回的是空指针 */
    //pthread_exit(NULL);
    /* pthread_exit((void*)1); */
    pthread_exit((void*)pVal);
}

int main()
{
    pthread_t pthid = 0;
    ///经过pthread_create函数之后，pthid的值会进行修改
    pthread_create(&pthid, NULL, start_routine, NULL);

    //打印pthid是在主线程中进行的
    printf("main: sub thread id is %ld\n", pthid);
    printf("main thread id is %ld\n", pthread_self());

    printf("main thread is joining\n");
    void * ret = NULL;
    pthread_join(pthid, &ret);

    //以16进制的形式打印ret的值
    printf("ret: %p\n", ret);
    //该值代表的是一个地址，是否是一个合法的地址，
    //还需要继续进行验证
    int * p = (int *)ret;
    printf("*p: %d, p: %p\n", *p, p);
    free(p);//在主线程中回收

    printf("main thread is exiting\n");
    return 0;
}

